export const db = wx.cloud.database()

class JTCollection {
  constructor(collectionName) {
    this.collection = db.collection(collectionName)
  }

  // 增删改查
  add(data) {
    return this.collection.add({
      data
    })
  }

  remove(condition, isDoc = true) {
    if (isDoc) {
      return this.collection.doc(condition).remove()
    } else {
       return this.collection.where({condition}).remove()
    }
  }

  update(condition,  data, isDoc = true) {
    if (isDoc) {
      return this.collection.doc(condition).update({data})
    } else {
      return this.collection.where({condition}).update({data})
    }
  }

  query(offset = 0, size = 20, condition = {}, isDoc = false) {
    if (isDoc) {
      return this.collection.doc(condition).get()
    } else {
      return this.collection.where(condition).skip(offset).limit(size).get()
    }
  }

}

export const favorCollection = new JTCollection('c_favor');
export const likeCollection = new JTCollection('c_like');
export const historyCollection = new JTCollection('c_history')
export const menuCollection = new JTCollection('c_menu')